package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public IPage<Item> getItemList(Integer pageNumber, Integer pageSize) {
        Page<Item> itemPage = new Page<>(pageNumber,pageSize);
        IPage<Item> list = itemMapper.selectPage(itemPage,null);
        return list;
    }

    /**
     * 新增
     * @param item
     */
    @Override
    public void saveItem(Item item) {
        itemMapper.insert(item);

        //发送消息es更新数据
        String exchangeItem = "item.topic";
        String routingKey ="item.insert";
        rabbitTemplate.convertAndSend(exchangeItem,routingKey,item.getId());
        System.out.println("es状态更新成功");
    }

    /**
     * 更新状态
     * @param id
     * @param status
     */
    @Override
    public void updateById(Long id, Integer status) {
        Item item = new Item();
        item.setUpdateTime(new Date());
        item.setStatus(status);
        item.setId(id);
        itemMapper.updateById(item);

        //发送消息es更新数据
        String exchangeItem = "item.topic";
        String routingKey =status==1?"item.up":"item.down";
        rabbitTemplate.convertAndSend(exchangeItem,routingKey,id);
        System.out.println("es状态更新成功");
    }

    /**
     * 修改
     * @param item
     */
    @Override
    public void updateAllById(Item item) {
        item.setUpdateTime(new Date());
        item.setStatus(null);
        itemMapper.updateById(item);

        //发送消息es更新数据
        String exchangeItem = "item.topic";
        String routingKey ="item.update";
        rabbitTemplate.convertAndSend(exchangeItem,routingKey,item.getId());
        System.out.println("es状态更新成功");
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        itemMapper.deleteById(id);

        //发送消息es更新数据
        String exchangeItem = "item.topic";
        String routingKey ="item.delete";
        rabbitTemplate.convertAndSend(exchangeItem,routingKey,id);
        System.out.println("es状态更新成功");
    }

    /**
     * 根据id查询一个
     * @param id
     */
    @Override
    public Item findById(Long id) {
        return itemMapper.selectById(id);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @Override
    public void deduct(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        int stock = item.getStock();
        if (stock >= num) {
            item.setStock(item.getStock()-num);
            itemMapper.updateById(item);
        }else{
            throw new RuntimeException("没库存了");
        }
    }
}
